﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Abp.Events.Bus;
using Abp.Events.Bus.Entities;
using Abp.Events.Bus.Handlers;
using Abp.RemoteEventBus;
using Abp.Runtime.Caching;
using FwProject.Comm;
using FwProject.Core.Distribution;
using FwProject.Core.Restaurant;
using FwProject.EventBusData;
using FwProject.Manager;

namespace FwProject.Application.EventHandle
{
    public class DistributionInfoDetailsHandle : IEventHandler<EntityChangedEventData<DistributionInfoDetails>>, IEventHandler<EntityCreatedEventData<DistributionInfoDetails>>,ITransientDependency
    {
        public readonly ICacheManager _catchmanager;
        public IRemoteEventBus remoteEventBus { get; set; }//缓存对象
        public readonly IRepository<DistributionInfo, Guid> _repositoryDistributionInfo;
        public readonly IRepository<DistributionInfoDetails, Guid> _repositoryDistributionInfoDetails;
        private readonly LoadingCarManager _loadingCarManager;
        public IEventBus EventBus { get; set; }
        public DistributionInfoDetailsHandle(ICacheManager catchmanager,
                                           IRepository<DistributionInfo, Guid> repositoryDistributionInfo,
                                           LoadingCarManager loadingCarManager,
                                           IRepository<DistributionInfoDetails, Guid> repositoryDistributionInfoDetails)
        {
            _loadingCarManager = loadingCarManager;
            _repositoryDistributionInfo = repositoryDistributionInfo;
            _repositoryDistributionInfoDetails = repositoryDistributionInfoDetails;
            _catchmanager = catchmanager;
            remoteEventBus = NullRemoteEventBus.Instance;
            EventBus = NullEventBus.Instance;

        }
        [UnitOfWork]
        public void HandleEvent(EntityCreatedEventData<DistributionInfoDetails> eventData)
        {
            ///添加配送信号
            CreatedDistributionDetailsEventData data = new CreatedDistributionDetailsEventData();
            data.Id = eventData.Entity.DistributionInfoId.Value;
            data.RestaurantId = eventData.Entity.Restaurantid;
            data.ActionType = ActionType.添加配送型号;
            data.EventDataDistributionInfoDetails = eventData.Entity;
            EventBus.Trigger(data);
            //  var dtModl = _repositoryDistributionInfo.GetAll().Where(m => m.Id == eventData.Entity.DistributionInfoId).FirstOrDefault();

        }
        [UnitOfWork]
        public void HandleEvent(EntityChangedEventData<DistributionInfoDetails> eventData)
        {
            ICache cache = _catchmanager.GetCache(nameof(DistributionInfoDetails));
            cache.Remove("list");
         
        }
    }
}
